package com.eorchis.module.preauthorizationregistration.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.basedao.dao.impl.HibernateAbstractBaseDao;
import com.eorchis.module.TrainingConstants;
import com.eorchis.module.interioruser.domain.UserInfoBean;
import com.eorchis.module.preauthorizationregistration.dao.IPreAuthorizationRegistrationDao;
import com.eorchis.module.preauthorizationregistration.domain.PreAuhorzaionQueryBean;
import com.eorchis.module.preauthorizationregistration.domain.PreAuthorizationRegistration;

import com.eorchis.core.basedao.query.condition.IPagingQueryCondition;
import com.eorchis.core.basedao.query.condition.builder.CompareType;
import com.eorchis.core.basedao.query.condition.builder.IConditionBuilder;
import com.eorchis.core.basedao.query.condition.builder.impl.DefaultQueryConditionBuilder;

import com.eorchis.core.page.commond.SortInfoBean;
import com.eorchis.core.ui.commond.IQueryCommond;

import com.eorchis.module.preauthorizationregistration.ui.commond.PreAuthorizationRegistrationQueryCommond;
import com.eorchis.module.preauthorizationregistration.ui.commond.PreAuthorizationRegistrationValidCommond;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: PreAuthorizationRegistrationDaoImpl<br>
 * Description: 个人报名预授权数据接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Administrator
 * @createDate 2015-01-06
 * @version $Revision$
 */
@SuppressWarnings("unchecked")
@Repository(value = "com.eorchis.module.preauthorizationregistration.dao.impl.PreAuthorizationRegistrationDaoImpl")
public class PreAuthorizationRegistrationDaoImpl extends HibernateAbstractBaseDao implements IPreAuthorizationRegistrationDao {
	
	@Override
	public Class<? extends IBaseEntity> entityClass() {
		return PreAuthorizationRegistration.class;
	}

	@Override
	public void queryConditionProcessor(
			IConditionBuilder defaultQueryConditionBuilder,
			IQueryCommond queryCommond) {
		PreAuthorizationRegistrationQueryCommond preAuthorizationRegistrationQueryCommond = (PreAuthorizationRegistrationQueryCommond)queryCommond;
		defaultQueryConditionBuilder.setBaseQueryString("SELECT t FROM PreAuthorizationRegistration t");
		defaultQueryConditionBuilder.addCondition("t.id", CompareType.IN, preAuthorizationRegistrationQueryCommond.getSearchIds());
		defaultQueryConditionBuilder.addCondition("t.user.userId", CompareType.EQUAL, preAuthorizationRegistrationQueryCommond.getSearchUserId());
		defaultQueryConditionBuilder.addCondition("t.year", CompareType.EQUAL, preAuthorizationRegistrationQueryCommond.getSearchYear());
		defaultQueryConditionBuilder.addCondition("t.department.depid", CompareType.EQUAL, preAuthorizationRegistrationQueryCommond.getSearchDeptId());
		defaultQueryConditionBuilder.addCondition("t.loginId", CompareType.IN, preAuthorizationRegistrationQueryCommond.getSearchLoginIds());
	}
	/**
	 * 根据部门id和登陆号查询此部门是否存在此用户
	 * @param String
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @date 2015-1-6
	 */
	@Override
	public List<UserInfoBean> findBaseUser(String loginId,String depId) throws Exception {
		StringBuffer hql = new StringBuffer("select u.user_name as \"userName\",u.login_id as \"loginID\",u.userid as \"userId\", bt.depid as \"depID\" ,bt.treepath as \"treepath\" " +
				" from base_department_user du, base_department bt, base_user u where bt.depid = du.depid " +
				" and du.userid = u.userid and du.active_state ="+TrainingConstants.IS_ACTIVE_Y+" and u.active_state ="+TrainingConstants.IS_ACTIVE_Y+" ");
		Map<String, Object> values = new HashMap<String, Object>();
		if(PropertyUtil.objectNotEmpty(depId)){
			hql.append(" and bt.depid = :depId");
			values.put("depId", depId);
		}
		if(PropertyUtil.objectNotEmpty(loginId)){
			hql.append(" and u.login_id = :loginId");
			values.put("loginId", loginId);
		}
		IConditionBuilder defaultQueryConditionBuilder  = new DefaultQueryConditionBuilder();
		defaultQueryConditionBuilder.setBaseQueryString(hql.toString());
		defaultQueryConditionBuilder.setQueryStringType(QueryStringType.SQL);
		defaultQueryConditionBuilder.setResultObject(UserInfoBean.class);
		IPagingQueryCondition queryCondition = (IPagingQueryCondition) defaultQueryConditionBuilder.buliderQueryCondition();
		queryCondition.setParameters(values);
		return (List<UserInfoBean>) this.findList(queryCondition);
	}
	/**
	 * 查询某部门所有已预授权的人的信息
	 * @param String
	 * @return
	 * @throws Exception
	 * @author Xupp
	 * @date 2015-1-8
	 */
	public List<PreAuhorzaionQueryBean> findPreAuthorZationRegistrationInfolist(PreAuthorizationRegistrationQueryCommond qCommond)throws Exception{
		StringBuffer hql=new StringBuffer();
		hql.append("select p.user.userId as userId,p.loginId as loginId,p.userName as userName,p.department.depname as deptName from PreAuthorizationRegistration p");
		hql.append(" where p.department.depid =:depid");
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("depid",qCommond.getSearchDeptId());
		// 增加排序
		if (qCommond.getSortInfos() != null) {
			SortInfoBean sortInfoBean = qCommond.getSortInfos();
			if (PropertyUtil.objectNotEmpty(sortInfoBean.getProperty())) {
				hql.append(" order by " + sortInfoBean.getProperty().trim());
				if (PropertyUtil.objectNotEmpty(sortInfoBean.getDirection())) {
					hql.append(" " + sortInfoBean.getDirection().trim());
				}
			}
		}
		IConditionBuilder defaultQueryConditionBuilder  = new DefaultQueryConditionBuilder();
		defaultQueryConditionBuilder.setBaseQueryString(hql.toString());
		defaultQueryConditionBuilder.setQueryStringType(QueryStringType.HQL);
		defaultQueryConditionBuilder.setResultObject(PreAuhorzaionQueryBean.class);
		IPagingQueryCondition queryCondition = (IPagingQueryCondition) defaultQueryConditionBuilder.buliderQueryCondition();
		queryCondition.setParameters(params);
		List<PreAuhorzaionQueryBean> list=(List<PreAuhorzaionQueryBean>) this.findList(queryCondition);
		return list;
	}
	/**
	 * 修改某部门某已预授权的人的姓名
	 * @param String
	 * @return
	 * @throws Exception
	 * @author Xupp
	 * @date 2015-1-8
	 */
	public int updatePreAuthorZationRegistrationInfolist(PreAuthorizationRegistrationValidCommond vCommond)throws Exception{
		StringBuffer sql=new StringBuffer();
		sql.append(" update PRE_AUTHORIZATION_REGISTRATION p set p.USER_NAME =:userName where p.USER_ID =:userId");
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("userName", vCommond.getUserName());
		params.put("userId",vCommond.getUser().getUserId());
		Integer count=super.executeUpdate(QueryStringType.SQL, sql.toString(), params);
		return count;
	}
	/**
	 * 撤销某部门某已预授权的人
	 * @param String
	 * @return
	 * @throws Exception
	 * @author Xupp
	 * @date 2015-1-8
	 */
	@Override
	public int deletePreAuthorZationRegistrationInfolist(
			PreAuthorizationRegistrationQueryCommond qCommond) throws Exception {
		StringBuffer hql=new StringBuffer();
		hql.append(" delete PreAuthorizationRegistration p where p.user.userId in (:userId) ");
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("userId", qCommond.getSearchUserIds());
		int count=super.executeUpdate(QueryStringType.HQL, hql.toString(), params);
		return count;
	}
	/**
	 * 预修改某部门某已预授权的人
	 * @param String
	 * @return
	 * @throws Exception
	 * @author Xupp
	 * @date 2015-1-8
	 */
	@Override
	public PreAuhorzaionQueryBean prePreAuthorZationRegistrationInfo(
			PreAuthorizationRegistrationValidCommond vCommond) throws Exception {
		StringBuffer hql=new StringBuffer();
		hql.append("select p.user.userId as userId,p.loginId as loginId,p.userName as userName,p.department.depname as deptName from PreAuthorizationRegistration p");
		hql.append(" where p.user.userId = :userId ");
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("userId", vCommond.getUser().getUserId());
		IConditionBuilder defaultQueryConditionBuilder  = new DefaultQueryConditionBuilder();
		defaultQueryConditionBuilder.setBaseQueryString(hql.toString());
		defaultQueryConditionBuilder.setQueryStringType(QueryStringType.HQL);
		defaultQueryConditionBuilder.setResultObject(PreAuhorzaionQueryBean.class);
		IPagingQueryCondition queryCondition = (IPagingQueryCondition) defaultQueryConditionBuilder.buliderQueryCondition();
		queryCondition.setParameters(params);
		List<PreAuhorzaionQueryBean> list=(List<PreAuhorzaionQueryBean>) this.findList(queryCondition);
		return list.get(0);
	}
	
}
